﻿using System;
using System.Windows.Data;

namespace WSManager.Wpf.WpfHelper
{
    public sealed class StringShortener: IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var str = value as string;
            if (str == null)
                return null;
            var length = parameter is int ? (int)parameter : 100;
            if(length < 3)
                throw new IndexOutOfRangeException();
            if (str.Length > length)
                return str.Substring(0, length - 3) + "...";
            return str;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
